package com.ihrm.company.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Company;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.response.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//1..解决跨域
@CrossOrigin
//2.设置restController
@RestController
//3.设置父路径
@RequestMapping(value = "/company")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;
    @Autowired
    CompanyService companyService;
    /**
     * 保存
     * @return
     */
    @RequestMapping(value = "/department",method = RequestMethod.POST)
    public Result save(@RequestBody Department department){
        //1.设置保存的企业IID
        String companyId="1";
        department.setCompanyId(companyId);
        //2.调用service完成保存企业
        departmentService.save(department);
        //3.构造返回结果
        return new Result(ResultCode.SUCCESS);
    }
    //查询企业的部门列表
    //指定企业id
    @RequestMapping(value = "/department",method = RequestMethod.GET)
    public Result  findAll(){
        //1.指定企业id
        String companyId="1";
        Company company = companyService.findById(companyId);
        //2.完成查询
        List<Department> list = departmentService.findAll(companyId);
        //3.构造返回及结果
        DeptListResult deptListResult=new DeptListResult(company,list);

        return new Result(ResultCode.SUCCESS,deptListResult);

    }
    //根据id查询department
    @RequestMapping(value = "/department/{id}",method = RequestMethod.GET)
    public Result  findById(@PathVariable(value = "id") String id){
        Department department = departmentService.findById(id);
        return new Result(ResultCode.SUCCESS,department);
    }
    //修改department
    @RequestMapping(value = "/department/{id}",method = RequestMethod.PUT)
    public  Result  update(@PathVariable(value = "id")String id,@RequestBody Department department){
        //设置修改的部门 ID
        department.setId(id);
        //调用service进行更新

        departmentService.update(department);
        return new Result(ResultCode.SUCCESS);
    }
    //根据ID进行删除
    @RequestMapping(value = "/department/{id}",method = RequestMethod.DELETE)
    public  Result  delete(@PathVariable(value = "id")String id){
        departmentService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }
}
